﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using haptic.src.engine.debug;
using haptic.src.engine.render;
using haptic.src.engine.core;

namespace haptic.src.engine
{
    [Serializable]
    public class Entity : Loadable, IXmlSerializable
    {
        #region Init
        public void Init(Scene oScene)
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.Init(oScene);
            }
        }
        #endregion
        #region Update
        public void MoveTo(Vector2 vPos)
        {
            m_vPosition = vPos;
        }
        public void TeleportTo(Vector2 vPos)
        {
            m_vPosition = vPos;
        }
        public void Update(GameTime oTime)
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.Update(oTime);
            }
        }
        public void PostUpdate(GameTime oTime)
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.PostUpdate(oTime);
            }
        }
        #endregion
        #region Load
        public Result PreLoad(Scene oScene)
        {
            foreach (Component oComp in m_oComponents)
            {
                if (oComp.PreLoad(oScene).IsFailed())
                    return new Failed();
            }
            return Result.Success;
        }
        public Result Load(Scene oScene, ContentManager oContent)
        {
            foreach (Component oComp in m_oComponents)
            {
                if (oComp.Load(oScene, oContent).IsFailed())
                    return new Failed();
            }
            return Result.Success;
        }
        public Result PostLoad(Scene oScene)
        {
            foreach (Component oComp in m_oComponents)
            {
                if (oComp.PostLoad(oScene).IsFailed())
                    return new Failed();
            }
            return Result.Success;
        }
        public void Unload(Scene oScene)
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.Unload(oScene);
            }
        }
        public void Start()
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.Start();
            }
        }
        #endregion
        #region Draw
        public void Draw(Renderer oRenderer, GameTime oTime)
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.Draw(oRenderer, oTime);
            }
        }
        public void DrawDebug(DebugRenderer oDebug, GameTime oTime)
        {
            foreach (Component oComp in m_oComponents)
            {
                oComp.DrawDebug(oDebug, oTime);
            }
        }
        #endregion
        #region Edit
        public Component GetComponent(string sTypeName)
        {
            foreach(Component oComp in m_oComponents)
            {
                if (oComp.GetType().Name == sTypeName)
                    return oComp;
            }
            return null;
        }
        public void AddComponent(Component oComponent)
        {
            m_oComponents.Add(oComponent);
        }
        public void RemoveComponent(string sTypeName)
        {
            foreach (Component oComp in m_oComponents)
            {
                if (oComp.GetType().ToString() == sTypeName)
                {
                    m_oComponents.Remove(oComp);
                    break;
                }
            }
        }
        public void RemoveComponent(Component oComponent)
        {
            m_oComponents.Remove(oComponent);
        }
        #endregion
        #region Serialization
        public XmlSchema GetSchema()
        {
            return null;
        }
        public void WriteXml(XmlWriter w)
        {
            // layer
            w.WriteAttributeString("layer", m_iLayer.ToString());

            // position
            w.WriteStartElement("position");
            w.WriteAttributeString("X", m_vPosition.X.ToString());
            w.WriteAttributeString("Y", m_vPosition.Y.ToString());
            w.WriteEndElement();

            // size
            w.WriteStartElement("size");
            w.WriteAttributeString("X", m_vSize.X.ToString());
            w.WriteAttributeString("Y", m_vSize.Y.ToString());
            w.WriteEndElement();

            // components
            w.WriteStartElement("components");
            foreach (Component oComp in m_oComponents)
            {
                w.WriteStartElement("component");
                w.WriteAttributeString("type", oComp.GetType().ToString());
                XmlSerializer oSrlz = new XmlSerializer(oComp.GetType());
                oSrlz.Serialize(w, oComp);
                w.WriteEndElement();
            }
            w.WriteEndElement();
        }
        public void ReadXml(XmlReader r)
        {
            // layer
            m_iLayer = int.Parse(r.GetAttribute("layer"));

            // position
            if (r.ReadToFollowing("position"))
            {
                m_vPosition.X = float.Parse(r.GetAttribute("X"));
                m_vPosition.Y = float.Parse(r.GetAttribute("Y"));
            }
            
            // size
            if (r.ReadToFollowing("size"))
            {
                m_vSize.X = float.Parse(r.GetAttribute("X"));
                m_vSize.Y = float.Parse(r.GetAttribute("Y"));
            }

            // components
            m_oComponents.Clear();
            if (r.ReadToFollowing("components"))
            {
                XmlReader r2 = r.ReadSubtree();
                while (r2.ReadToFollowing("component"))
                {
                    Type oType = Type.GetType(r2.GetAttribute("type"));
                    XmlSerializer oSrlz = new XmlSerializer(oType);
                    r2.ReadToFollowing(oType.Name);
                    Component oComp = (Component)oSrlz.Deserialize(r2);
                    m_oComponents.Add(oComp);
                }
            }
        }
        #endregion
        #region Attributes
        [XmlElement("position")]
        public Vector2 m_vPosition = Vector2.Zero;
        [XmlElement("size")]
        public Vector2 m_vSize = new Vector2(10, 10);
        [XmlAttribute("layer")]
        public int m_iLayer = 1000;
        private List<Component> m_oComponents = new List<Component>();
        public List<Component> components { get { return m_oComponents; } }
        #endregion
    }
}
